#include "mainwindow.h"
#include "ui_mainwindow.h"



#include<QMessageBox>
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QTabWidget>
#include <QVBoxLayout>
#include <QLabel>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{

    ui->setupUi(this);
    ui->Value->setText("0");
       // QString m_sXml = m_tcpRecText->toPlainText();
   // ui->textEditmycal.verticalScrollBar()->setValue(0);
    //add QHBoxLayout
    QHBoxLayout * qlayout = new QHBoxLayout(this);
    //add tabWidget
    tabWidget = new QTabWidget(this);

    tabWidget->setTabPosition(QTabWidget::North);
    tabWidget->setTabShape(QTabWidget::Rounded);
    //tabWidget->set
    // tabWidget->setColumnWidth
    tabWidget->setDocumentMode(true);
    tabWidget->setFixedHeight(200);
    tabWidget->setFixedWidth(1920);

    //construct newcanselinfopage
    NewCanSelInfoPage *instancenewcanselinfopage =  NewCanSelInfoPage::getInstance();
    tabWidget->insertTab(0,instancenewcanselinfopage,tr("File"));
    tabWidget->setTabToolTip(1,"file");


    //constuct cananalysis
    Cananalysis *instanceanalysis = Cananalysis::getInstance();
    tabWidget->insertTab(1,instanceanalysis,tr("analysis"));
    tabWidget->setTabToolTip(2,"analysis");
    tabWidget->setCurrentIndex(1);
    qlayout->addWidget(tabWidget);

    //    //constuct cananalysis
    canig *instanceCanIGBus = canig::getInstance();
    tabWidget->insertTab(2,instanceCanIGBus,tr("CNode IG"));
    tabWidget->setTabToolTip(3,"analysis");
    tabWidget->setCurrentIndex(1);
    qlayout->addWidget(tabWidget);
    // add qtabwidget ti mainwindow
    this->setLayout(qlayout);

    setWindowTitle("QThreadPool Example");
    resize(400, 200);



}



MainWindow::~MainWindow()
{
    qDebug()<<"delete ui ";
    delete ui;
    //删除
    delete workethread;
    qDebug()<<"delete workthread";
}



void MainWindow::on_pushButtonstart_clicked()
{

    workethread = new WorkThread();
    sendthread = new SendThread();
    workethread->start();
    sendthread->start();
    connect(workethread, SIGNAL(currentValue_signal(  QVector<ThreadInterface::MyStruct>,int)), this, SLOT(showvalue(  QVector<ThreadInterface::MyStruct>,int)));
    connect(sendthread, SIGNAL(currentValue_signal(  QVector<ThreadInterface::MyStruct>,int)), this, SLOT(showvalue_send(  QVector<ThreadInterface::MyStruct>,int)));
    ui->pushButtonstart->setEnabled(false);
    ui->pushButton_end->setEnabled(true);

}

void MainWindow::showvalue(QVector<ThreadInterface::MyStruct> value, int n)
{
    QString value1;

    foreach(const ThreadInterface::MyStruct &s, value) {
        qDebug() << "text >>>ID:" << s.id << ", Name:" << s.name;
        value1 = value1 +QString::number(s.id) +"-"+s.name+"]";
    }
    ui->Value->setText("叶韵诗:>>>>"+value1+"*****"+QString::number(n)); //更新label内容
    ui->textEditmycal->append("叶韵诗:>>>>"+value1+"*****"+QString::number(n));
//# 文本编辑框自动往下滚动
  //  ui->textEditmycal->verticalScrollBar().setValue(  ui->textEditmycal->verticalScrollBar().maximum());
   // ui->textEditmycal->moveCursor(QTextCursor::End);
}

void MainWindow::showvalue_send(QVector<ThreadInterface::MyStruct> value, int n)
{
    QString value1;

    foreach(const ThreadInterface::MyStruct &s, value) {
        qDebug() << "text >>>ID:" << s.id << ", Name:" << s.name;
        value1 = value1 +QString::number(s.id) +"-"+s.name+"]";
    }
  //  ui->Value->setText("yangxinxin:>>>>"+value1+"*****"+QString::number(n)); //更新label内容
    ui->textEditmysend->append("yangxinxin:>>>>"+value1+"*****"+QString::number(n));
}


void MainWindow::on_pushButton_end_clicked()
{
    workethread->terminate();
    workethread->wait();
    sendthread->terminate();
    sendthread->wait();
    ui->pushButtonstart->setEnabled(true);
    ui->pushButton_end->setEnabled(false);

}


void MainWindow::on_textEditmycal_textChanged()
{
    ui->textEditmycal->moveCursor(QTextCursor::End);
}


void MainWindow::on_textEditmysend_textChanged()
{
    ui->textEditmysend->moveCursor(QTextCursor::End);
}

